package com.qch.controller;


import com.qch.config.RabitConfig;
import com.qch.pojo.OrderDO;
import com.qch.service.OrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author qch
 * @Date 2021/5/20
 */
@RestController
public class OrderController {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Autowired
    private OrderService orderService;
    /**当产生订单时，加入到订单表，同时加入订单队列
    * 当订单超时10分钟后加入到死信队列，通过监听死信队列，将订单状态置为已取消
     **/
   /* @RequestMapping("/order")
   public String order()
   {
       //生成主键订单id
       String orderId = UUID.randomUUID().toString().replace("-","");
       rabbitTemplate.convertAndSend(RabitConfig.BIZ_ORDER_EXCHANGE,RabitConfig.BIZ_ORDER_ROUTING_KEY,orderId);
       //加入队列
       return "您的订单号是"+orderId+",请在30秒内完成支付！";
   }*/
    @RequestMapping("/order")
    public ModelAndView order()
    {
        ModelAndView modelAndView=new ModelAndView("order.html");
        //生成主键订单id
        String orderId = UUID.randomUUID().toString().replace("-","");
        OrderDO orderDO=new OrderDO();
        orderDO.setCreateBy("qch");
        orderDO.setCreateAt(new Date());
        orderDO.setUpdateAt(new Date());
        orderDO.setOrderId(orderId);
        orderDO.setStatus("待支付");
        orderService.insert(orderDO);
        rabbitTemplate.convertAndSend(RabitConfig.BIZ_ORDER_EXCHANGE,RabitConfig.BIZ_ORDER_ROUTING_KEY,orderId);
        //加入队列

        String msg="您的订单号是"+orderId+",请在30秒内完成支付！";
        modelAndView.addObject("msg",msg);
        modelAndView.addObject("orderId",orderId);
        return modelAndView;
    }
  @RequestMapping("/pay/{orderId}")
    public String pay(@PathVariable String orderId)
    {
        /**
         * 订单状态：
         * 1.待支付
         * 2.已支付
         * -1.超时已取消
         */
        OrderDO orderDO = orderService.queryByOrderId(orderId);
        if(orderDO.getStatus().equals("超时已取消"))//订单状态为已超时
        {
            return "-1";
        }else if(orderDO.getStatus().equals("已支付")){
            return "2";

        }else {
            orderDO.setStatus("已支付");
            orderDO.setUpdateAt(new Date());
            orderService.update(orderDO);
        }
        //加入队列
        return "1";
    }
   @RequestMapping("/success")
   public ModelAndView success()
   {
       ModelAndView modelAndView=new ModelAndView();

       modelAndView.setViewName("success.html");
       return modelAndView;
   }
    @RequestMapping("/fail")
    public ModelAndView fail()
    {
        ModelAndView modelAndView=new ModelAndView();
        List<OrderDO> orderList =new ArrayList<>();
        orderList=orderService.queryAll("qch");
        modelAndView.addObject("orderList",orderList);
        modelAndView.setViewName("failList.html");
        return modelAndView;
    }



}
